<template>
  <div>
    <el-card>
      <!-- 查询区 -->
      <el-form size="small" inline :model="queryInfo" ref="queryFormRef">
        <el-form-item label="部门名称" prop="deptId" size="medium">
          <dept-select :value.sync="queryInfo.deptId" style="width:217px" />
        </el-form-item>
        <el-form-item label="员工名称" prop="empName" size="medium">
          <el-input
            v-model="queryInfo.empName"
            @keyup.enter.native="handleQuery"
            clearable
            placeholder="请输入员工姓名搜索"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="handleReset('queryFormRef')">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="employeesData"
        tooltip-effect="dark"
        :header-cell-style="{ background: '#f8f8f9', color: '#515a6e' }"
        style="width: 100%;"
        v-loading="loading"
      >
        <el-table-column label="员工编号" prop="id" width="120" align="center"></el-table-column>
        <el-table-column label="员工姓名" prop="empName" align="center"></el-table-column>
        <el-table-column label="部门名称" prop="deptName" align="center"></el-table-column>
        <el-table-column label="岗位名称" prop="staName" align="center"></el-table-column>
        <el-table-column label="调动操作">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" @click="addStaTranForm(scope.row)">调动</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="getEmployeeInfoList"
        @current-change="getEmployeeInfoList"
        :current-page.sync="queryInfo.pageNum"
        :page-sizes="[10, 20, 30]"
        :page-size.sync="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <el-dialog title="岗位调动" :visible.sync="dialogVisible">
      <el-form
        :model="staTranForm"
        inline
        label-width="100px"
        label-position="right"
        ref="staTranFormRef"
        @close="dialogClose"
        :rules="staTranFormRules"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工编号" prop="empId">
              <el-input v-model="staTranForm.empId" placeholder="请输入员工编号" clearable disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工姓名" prop="empName">
              <el-input v-model="staTranForm.empName" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工岗位编号" prop="preStaId">
              <el-input v-model="staTranForm.preStaId" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工岗位名称" prop="preStaName">
              <el-input v-model="staTranForm.preStaName" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工新岗位" prop="newStaId">
              <el-select v-model="staTranForm.newStaId" clearable>
                <el-option
                  v-for="item in options"
                  :value="item.id"
                  :label="item.staName"
                  :key="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调转类型" prop="traType">
              <el-select v-model="staTranForm.traType">
                <el-option label="升职" :value="1"></el-option>
                <el-option label="降职" :value="2"></el-option>
                <el-option label="数据库错误" :value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="调转原因" prop="traReason">
              <el-input
                v-model="staTranForm.traReason"
                type="textarea"
                :rows="2"
                placeholder="请输入调转原因"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="traRemark">
              <el-input
                v-model="staTranForm.traRemark"
                type="textarea"
                :rows="2"
                placeholder="请输入备注"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addStaTranApi } from "@/api/stationTran";
import { listStaBy } from "@/api/station";
import { listEmployee, getEmpToDeptLog } from "@/api/employee";
export default {
  data() {
    return {
      queryInfo: {
        // 当前页
        pageNum: 1,
        // 页大小
        pageSize: 10,

        //部门编号
        deptId: undefined,
        //员工姓名
        empName: "",
      },
      employeesData: [],
      dialogVisible: false,
      staTranForm: {
        empId: "",
        empName: "",
        preStaId: "",
        preStaName: "",
        newStaId: "",
        traType: "",
        traReason: "",
        traRemark: "",
      },
      loading: false,
      total: 0,
      options: [],
      staTranFormRules: {
        newStaId: [
          {
            required: true,
            message: "请选择员工岗位",
            trigger: "blur",
          },
        ],
        traType: [
          {
            required: true,
            message: "请选择调转类型",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    async getEmployeeInfoList() {
      try {
        this.loading = true;
        const { data, code } = await listEmployee(this.queryInfo);
        if (code !== 200) return;
        this.employeesData = data.employeeVos;
        this.total = data.total;
      } finally {
        this.loading = false;
      }
    },
    handleQuery() {
      this.getEmployeeInfoList();
    },
    handleReset(formName) {
      this.$refs[formName].resetFields();
      this.getEmployeeInfoList();
    },
    addStaTranForm(tran) {
      this.dialogVisible = true;
      // console.log(tran.staId);
      this.getOptionsData(tran.staId);
      this.getEmployeeById(tran.id);
    },
    async getEmployeeById(id) {
      const { data, code } = await getEmpToDeptLog(id);
      if (code !== 200) return;
      this.staTranForm = data;
      this.staTranForm.empId = data.id;
      this.staTranForm.preStaId = data.staId;
      this.staTranForm.preStaName = data.staName;
      // console.log(this.staTranForm);
    },
    async getOptionsData(id) {
      const { code, data } = await listStaBy(id);
      if (code !== 200) return;
      this.options = data.stations;
    },
    handleSubmit() {
      this.$refs.staTranFormRef.validate(async (valid) => {
        if (!valid) return;
        const { code } = await addStaTranApi(this.staTranForm);
        if (code !== 200) return;
        this.$message.success("调转成功");
        this.dialogVisible = false;
        this.getEmployeeInfoList();
      });
    },
    dialogClose() {
      this.staTranForm = {};
    },
  },
  created() {
    this.getEmployeeInfoList();
  },
};
</script>

<style lang="scss" scoped>
.el-pagination {
  margin: 18px 0px;
  float: right;
}
</style>
